package icu.makka.yygh.cmn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import icu.makka.yygh.cmn.mapper.DictMapper;
import icu.makka.yygh.cmn.service.DictService;
import icu.makka.yygh.model.cmn.Dict;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    /**
     * 根据dictcode和value查询数据字典
     * @param dictCode
     * @param value
     * @return
     */
    @Override
    public String getDictName(String dictCode, String value) {
        log.info("业务层，根据dictcode和value查询数据字典");

        if (!StringUtils.isEmpty(dictCode)) {
            //如果字典编号非空，则根据dictcode和value查询
            Dict dict = getDictByDictCode(dictCode);
            //获取parent_id
            Long parentId = dict.getId();
            //根据parentId和value查询
            LambdaQueryWrapper<Dict> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(parentId != null, Dict::getParentId, parentId);
            queryWrapper.eq(!StringUtils.isEmpty(value), Dict::getValue, value);
            Dict dictOne = this.getOne(queryWrapper);

            //返回结果
            return dictOne.getName();
        } else {
            //如果字典编号为空，则根据value查询
            LambdaQueryWrapper<Dict> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(!StringUtils.isEmpty(value), Dict::getValue, value);
            Dict dict = this.getOne(queryWrapper);

            //返回结果
            return dict.getName();
        }
    }

    /**
     * 根据dictCode获取下级节点
     * @param dictCode
     * @return
     */
    @Override
    public List<Dict> findByDictCode(String dictCode) {
        log.info("业务层，根据dictCode获取下级节点，dictCode={}", dictCode);
        Dict dict = getDictByDictCode(dictCode);
        if (dict == null) {
            return null;
        } else {
            List<Dict> childData = findChildData(dict.getId());
            return childData;
        }
    }

    /**
     * 根据dictCode查询数据字典
     * @param dictCode
     * @return
     */
    private Dict getDictByDictCode(String dictCode) {
        log.info("根据dictCode查询数据字典，dictCode={}", dictCode);

        //执行查询
        LambdaQueryWrapper<Dict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(dictCode), Dict::getDictCode, dictCode);
        Dict dict = this.getOne(queryWrapper);

        log.info("查询结果Dict={}", dict);

        //返回结果
        return dict;
    }

    /**
     * 根据id查询子数据列表
     * @param id
     */
    @Override
    public List<Dict> findChildData(Long id) {
        log.info("业务层，根据id查询子数据列表，id={}", id);

        //根据id查询子数据
        LambdaQueryWrapper<Dict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null, Dict::getParentId, id);
        List<Dict> list = this.list(queryWrapper);

        //设置Dict结果集的hasChildren字段
        list.stream().forEach(dict -> {
            //含有子数据，flag为true，否则为false
            boolean flag = hasChildren(dict.getId());
            dict.setHasChildren(flag);
        });

        //返回结果
        return list;
    }

    /**
     * 判断是否含有子数据，是：true，否：false
     * @param id
     * @return
     */
    public boolean hasChildren(Long id) {
        log.info("业务层，判断是否含有子数据，id={}", id);

        //根据id查询子数据
        LambdaQueryWrapper<Dict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null, Dict::getParentId, id);
        int count = this.count(queryWrapper);

        //返回结果
        return count > 0;
    }
}
